package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.qo.MyAJAX;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    @RequiredPermission(name="投票列表",expression="activity:list")
    @RequestMapping("/投票列表")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "vote/投票列表";
    }


    @RequiredPermission(name="投票单删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "";
    }




    @RequiredPermission(name="投票页面",expression="activity:投票页面")
    @RequestMapping("/投票页面")
    public String input(Long id, Model model) {
        if (id != null) {
            Activity activity = activityService.get(id);
            activity.setItems(activityService.selectActivityItemAll(activity.getId()));
            model.addAttribute("activity",activity);
        }
        return "vote/投票页面";
    }

    @RequiredPermission(name="saveOrUpdate",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public MyAJAX saveOrUpdate(Integer  optionsRadios) {
        //业务方法
        activityService.VoteToComplete(optionsRadios);
        return new MyAJAX();
    }

    @RequiredPermission(name="投票结果",expression="activity:投票结果")
    @RequestMapping("/投票结果")
    public String result(Long id,Model model) {
        //业余方法
        Activity activityResult = activityService.selectResult(id);
        model.addAttribute("result",activityResult);
        return "vote/投票结果" ;
    }

    @RequiredPermission(name="examine",expression="activity:examine")
    @RequestMapping("/examine")
    @ResponseBody
    public MyAJAX examine(Long id) {
        try {
            //业余方法 检查有没有投票
            activityService.examine(id);
            return new MyAJAX() ;
        }catch (Exception e){
            return new MyAJAX(e.getMessage(),false);
        }
    }


}
